import openai
import re

# Authenticate with the OpenAI API using your API key
openai.api_key = "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"


# Define a function to analyze the user's input
def analyze_input(user_input):
    # Call the OpenAI API to analyze the input
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=user_input,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    # Extract the text from the OpenAI API response
    output_text = response.choices[0].text

    # Extract the sentiment and location entities from the text using regular expressions
    sentiment = re.search("情感：(.+?)，", output_text).group(1)
    location = re.search("地点：(.+?)。", output_text).group(1)

    # Return the sentiment and location
    return sentiment, location


# Ask the user for input and analyze it
user_input = input("请输入：")
sentiment, location = analyze_input(user_input)

# Print the sentiment and location
print("情感：%s，地点：%s" % (sentiment, location))
